package hander

import (
	"context"
	"github.com/gin-gonic/gin"
	"github.com/minio/minio-go/v7"
	"github.com/minio/minio-go/v7/pkg/credentials"
	"log"
	"math/rand"
	"net/http"
	"rpc-server/api/request"
	"rpc-server/basic/global"
	"rpc-server/model"
	"time"
)

func SendSms(c *gin.Context) {
	var req request.SendSms

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数验证错误",
			"data": nil,
		})
		return
	}

	code := rand.Intn(9000) + 1000
	global.Rdb.Set(context.Background(), "sendSms"+req.Tel, code, time.Minute*1)

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "发送验证码成功",
		//"data": set,
	})

}

func Login(c *gin.Context) {
	var req request.Login

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数验证错误",
			"data": nil,
		})
		return
	}

	get := global.Rdb.Get(context.Background(), "sendSms"+req.Tel)
	if get.Val() != req.SendSmsCode {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证码错误",
			"data": nil,
		})
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "登录成功",
		//"data": set,
	})
}

func List(c *gin.Context) {
	var req request.List
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数验证错误",
			"data": nil,
		})
		return
	}

	var video model.Videp
	page := req.Page
	if page <= 0 {
		page = 1
	}

	pageSize := req.Size
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	offset := (page - 1) * pageSize

	if err := global.DB.Order("create_at desc").Offset(int(offset)).Limit(int(pageSize)).First(&video).Error; err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "展示失败",
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": video,
	})

}

func Minio(c *gin.Context) {
	ctx := context.Background()
	endpoint := "video"
	accessKeyID := "minio_JspNRM"
	secretAccessKey := "cd423a2933d92b70"
	useSSL := true

	// Initialize minio client object.
	minioClient, err := minio.New(endpoint, &minio.Options{
		Creds:  credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
		Secure: useSSL,
	})
	if err != nil {
		log.Fatalln(err)
	}

	// Make a new bucket called testbucket.
	bucketName := "testbucket"
	location := "us-east-1"

	err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location})
	if err != nil {
		// Check to see if we already own this bucket (which happens if you run this twice)
		exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
		if errBucketExists == nil && exists {
			log.Printf("We already own %s\n", bucketName)
		} else {
			log.Fatalln(err)
		}
	} else {
		log.Printf("Successfully created %s\n", bucketName)
	}

	// Upload the test file
	// Change the value of filePath if the file is in another location
	objectName := "testdata"
	filePath := "/tmp/testdata"
	contentType := "application/octet-stream"

	// Upload the test file with FPutObject
	info, err := minioClient.FPutObject(ctx, bucketName, objectName, filePath, minio.PutObjectOptions{ContentType: contentType})
	if err != nil {
		log.Fatalln(err)
	}

	log.Printf("Successfully uploaded %s of size %d\n", objectName, info.Size)

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "上传成功",
	})

}

func Update(c *gin.Context) {
	var req request.Update

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数验证错误",
			"data": nil,
		})
		return
	}

	video := model.Videp{
		Id:     int(req.Id),
		Status: req.Status,
	}
	global.DB.Find(&video)

	if err := global.DB.Updates(&video); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "修改失败",
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "修改成功",
		//"data": set,
	})

}

func Del(c *gin.Context) {
	var req request.Del

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数验证错误",
			"data": nil,
		})
		return
	}

	var video model.Videp

	if err := global.DB.Delete(&video); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "删除成功",
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "删除成功",
		//"data": set,
	})
}
